﻿using System;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using WD.Shell.DrawingObjects;
using WD.Shell.Zoom;

namespace WD.Shell
{
    public partial class MainWindow
    {
        private double _xmin;
        private double _ymin = -1.1;
        private CustomLine _line;

        private readonly Timer _timer = new Timer();

        public MainWindow()
        {
            InitializeComponent();
            GraphData = new GraphControlData();
            DataContext = new MainWindowPresenter(GraphControl);
            _timer.Interval = 10;
            BindHandler();
        }

        private void BindHandler()
        {
            _timer.Elapsed += TimerElapsed;
            uiScaleSlider.MouseDoubleClick += RestoreScalingFactor;
            var helper = new PanHelper(GraphControl);
            var maghelper = new MagnificationHelper(GraphControl, magnifierEllipse, magnifierCanvas,uiScaleSlider);
        }

        private void RestoreScalingFactor(object sender, MouseButtonEventArgs e)
        {
            ((Slider)sender).Value = 1.0;
        }

        void TimerElapsed(object sender, ElapsedEventArgs e)
        {
            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(TimerWorkItem));
        }

        public void TimerWorkItem()
        {
            _line.IsCoSine = !_line.IsCoSine;
            _line.Draw();
        }

        public GraphControlData GraphData { get; set; }

        void DrawASineCurve()
        {
            _line = new CustomLine(GraphControl)
                {
                    Stroke = new SolidColorBrush(Colors.Black),
                    Xmin = _xmin,
                    Ymin = _ymin,
                    Opacity = 1,
                    AutoResize = true,
                    Margin = new Thickness(100)
                };
            _line.Draw();
            _xmin += 0.2;
            _ymin += -0.1;
        }

        private void CheckFps(object sender, RoutedEventArgs e)
        {
            var toggleButton = (ToggleButton)sender;
            if (toggleButton.IsChecked != null && toggleButton.IsChecked.Value)
            {
                _timer.Start();
            }
            else
            {
                _timer.Stop();
            }
        }

        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            DrawASineCurve();
        }

        protected override void OnPreviewMouseWheel(MouseWheelEventArgs args)
        {
            base.OnPreviewMouseWheel(args);
            if (Keyboard.IsKeyDown(Key.LeftCtrl) ||
                Keyboard.IsKeyDown(Key.RightCtrl))
            {
                uiScaleSlider.Value += (args.Delta > 0) ? 0.1 : -0.1;
            }
        }

        protected override void OnPreviewMouseDown(MouseButtonEventArgs args)
        {
            base.OnPreviewMouseDown(args);
            if (Keyboard.IsKeyDown(Key.LeftCtrl) ||
                Keyboard.IsKeyDown(Key.RightCtrl))
            {
                if (args.MiddleButton == MouseButtonState.Pressed)
                {
                    RestoreScalingFactor(uiScaleSlider, args);
                }
            }
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            if(e.Key == Key.Escape)
            {
                if (btnZoomer.IsChecked.HasValue && btnZoomer.IsChecked.Value)
                {
                    btnZoomer.IsChecked = false;
                }
            }
        }
    }
}
